/**
 * Created by Administrator on 2016/5/26.
 */
'use strict';

angular.module('swanPc')
    .controller('LoginController',function($rootScope,$scope,$state,$timeout,Auth){

        // /console.log(Auth.isLoggedIn());

        $scope.user={};
        $scope.errors={};
        $scope.rememberMe=true;
        $timeout(function(){angular.element('[ng-model="username"]').focus();});
        $scope.login=function(event){
            // alert("here");
            event.preventDefault();
            Auth.login({
                username:$scope.username,
                password:$scope.password,
                rememberMe:$scope.rememberMe
            }).then(function(){
                $state.go('calendar');
            }).catch(function(){
                $scope.autheticationError=true;
                //alert("login failed,please check your username or passord");
            })
        };
    })
    .controller('RegisterController', function ($scope, $translate, $timeout, Auth,RegisterAccount) {
        $scope.success = null;
        $scope.error = null;
        $scope.doNotMatch = null;
        $scope.errorUserExists = null;
        $scope.mobileNotNull = null;
        $scope.registerAccount = {
            login:null,
            password:null,
            verifyCode:null,
            mobile:""
        };
        //$timeout(function (){angular.element('[ng-model="registerAccount.login"]').focus();});

        //获取验证码
        $scope.getVerifyCode=function(){
            //手机号格式检测
            var pattern = /^1[3,5,8]\d{9}$/;
            $scope.sendVerifyCodeInfo={
                mobile:null,
                verifyFor:"register"
            };
            if($scope.registerAccount.mobile=="" || $scope.registerAccount ==null){
                swal("手机号不能为空，请输入手机号");
            }else if(!pattern.test($scope.registerAccount.mobile)){
                swal("请输入正确格式的手机号");
            }else{
                $scope.sendVerifyCodeInfo.mobile=$scope.registerAccount.mobile;
                RegisterAccount.save($scope.sendVerifyCodeInfo,function(result){
                    $scope.registerAccount.verifyCode=result.code;
                })
            }
        };


        $scope.register = function () {
            // console.log($scope.registerAccount);
            $scope.registerAccount.login = $scope.registerAccount.mobile;
            console.log($scope.registerAccount.password);
            if($scope.registerAccount.password==null || $scope.registerAccount.password==""){
                swal("注册密码不能为空");
            }
            Auth.createAccount($scope.registerAccount).then(function (result) {
                console.log(result);
                $scope.success = 'OK';
            }).catch(function (response) {
                $scope.success = null;
                //console.log("response.status:"+response.status);
                console.log("response.data:"+response.data.message);
                if (response.status === 400 && response.data === 'login already in use') {
                    $scope.errorUserExists = 'ERROR';
                } else if (response.status === 400 && response.data === 'e-mail address already in use') {
                    $scope.errorEmailExists = 'ERROR';
                } else {
                    $scope.error = 'ERROR';
                }
            });
        };
    })
    .controller("ResetPasswordController",function($scope,AccountPasswordInit,AccountPasswordInitFinish){
        $scope.resetPasswordEmail="";
        $scope.resetKey="";
        $scope.resetPasswordMessage={
            key:"",
            newPassword:""
        }
        $scope.newPassword="";
        $scope.newPasswordConfirm="";
        $scope.sendEmailKey=function(){
            if($scope.resetPasswordEmail==null || $scope.resetPasswordEmail==""){
                swal("邮箱不能为空，请重新输入");
            }
            AccountPasswordInit.save($scope.resetPasswordEmail,function(result){
                swal("邮箱验证码发送成功，请注意查看");
                //swapScreen("resetPassord");
            },function(result){
                swal("邮箱验证码发送失败");
            })
        }
        /*$scope.resetPasswordWithKey=function(){
            if($scope.newPassword==null || $scope.newPasswordConfirm==""){
                swal("新密码不能为空，请重新输入");
            }else if($scope.newPassword!=$scope.newPasswordConfirm){
                swal("两次密码输入不一致，请重新输入");
            }else {
                $scope.resetPasswordMessage.key = $scope.resetKey;
                $scope.resetPasswordMessage.newPassword = $scope.newPassword;
                AccountPasswordInitFinish.save($scope.resetPasswordMessage,function(result){
                    console.log(result);
                    swapScreen("login");
                    swal("密码重置成功","","success");
                },function(result){
                    console.log(result);
                    swal("密码重置失败","","error");
                })
            }
        }*/
    })
function swapScreen(id) {
    jQuery('.visible').removeClass('visible animated fadeInUp');
    jQuery('#'+id).addClass('visible animated fadeInUp');
}
/*jQuery(document).ready(function() {
 App.setPage("login");  //Set current page
 App.init(); //Initialise plugins and elements
 });
 */
